package com.work.xxzl.module.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.work.xxzl.R;
import com.work.xxzl.adapter.CommonAdapter;
import com.work.xxzl.adapter.CommonFragmentPagerAdapter;
import com.work.xxzl.adapter.ViewHolder;
import com.work.xxzl.base.BaseFragmentForMain;
import com.work.xxzl.module.pagehome.LoveWallFragment;
import com.work.xxzl.module.pagehome.MakeFriendsFragment;
import com.work.xxzl.module.pagehome.MatrimoniaFragment;
import com.work.xxzl.module.pagehome.SuggestFragment;
import com.work.xxzl.module.pagehome.TeacherFragment;
import com.work.xxzl.utils.UIUtils;
import com.work.xxzl.utils.ViewUtils;
import com.work.xxzl.view.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author WZ 2018/12/8
 * 附近的人
 */
public class SecondFragment extends BaseFragmentForMain {
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    private List<Fragment> fragments = new ArrayList<>();
    private String tabTitles[] = {"交友", "征婚", "幸福导师"};
    private CommonFragmentPagerAdapter adapter;
    private List<String> listdata = new ArrayList<>();
    private CommonPopupWindow commonPopupWindow;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_secondfragment_content;
    }

    @Override
    protected void initView() {
        setViewPagerTablayout();
        ViewUtils.setOnClickListeners(this, ivAdd);
    }

    @Override
    protected void initData() {
        super.initData();
        setPopupWindow();
    }

    //Tablayout和ViewPager
    private void setViewPagerTablayout() {
        MakeFriendsFragment makeFriendsFragment = new MakeFriendsFragment();
        MatrimoniaFragment matrimoniaFragment = new MatrimoniaFragment();
        TeacherFragment teacherFragment = new TeacherFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("key", 1);
        makeFriendsFragment.setArguments(bundle);
        matrimoniaFragment.setArguments(bundle);
        teacherFragment.setArguments(bundle);

        fragments.add(makeFriendsFragment);
        fragments.add(matrimoniaFragment);
        fragments.add(teacherFragment);
        adapter = new CommonFragmentPagerAdapter(getChildFragmentManager(), fragments, tabTitles);
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);
        viewpager.setCurrentItem(0);
        for (int i = 0; i < tabTitles.length; i++) {
            TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(i)).getChildAt(1));
            //选中字体样式
            if(i==0){
                title.animate().scaleX(1.0f);
                title.animate().scaleY(1.0f);
            }else {
                title.animate().scaleX(0.8f);
                title.animate().scaleY(0.8f);
            }
        }
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition());
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //选中字体样式
                title.animate().scaleX(1.0f);
                title.animate().scaleY(1.0f);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //未选中字体样式
                title.animate().scaleX(0.8f);
                title.animate().scaleY(0.8f);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.iv_add:
                //筛选
                backgroundAlpha(0.7f);
                commonPopupWindow.showAsDropDown(ivAdd, -UIUtils.dip2px(110), 5);
                break;

        }
    }

    //加号按钮弹框
    private void setPopupWindow() {
        if (listdata.size() == 0) {
            listdata.add("只看男生");
            listdata.add("只看女生");
            listdata.add("查看全部");
        }
        if (commonPopupWindow == null) {
            commonPopupWindow = new CommonPopupWindow(getActivity(), R.layout.layout_listview);
            commonPopupWindow.setWidth(UIUtils.dip2px(150));
            commonPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.messagebg));
        }
        View view = commonPopupWindow.getMenuView();
        ListView listView = view.findViewById(R.id.lv_listview);
        listView.setDividerHeight(2);
        listView.setPadding(20, 0, 0, 0);
        listView.setAdapter(new CommonAdapter<String>(getActivity(), listdata, R.layout.item_textview_withimage) {
            @Override
            public void convert(ViewHolder helper, String item, int position) {
                helper.setText(R.id.textview, item);
                switch (position) {
                    case 0:
                        helper.setImage(R.id.imageview, 0);
                        break;
                    case 1:
                        helper.setImage(R.id.imageview, 0);
                        break;
                    case 2:
                        helper.setImage(R.id.imageview, 0);
                        break;
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                commonPopupWindow.dismiss();
                switch (position) {
                    case 0:
                        //只看男生
                        break;
                    case 1:
                        //只看女生

                        break;
                    case 2:
                        //查看全部

                        break;
                }
            }
        });
        commonPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
    }

}
